package com.Txunda.fxw.ui.activity.other;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.Txunda.fxw.R;
import com.Txunda.fxw.base.utils.util.JSONUtils;
import com.Txunda.fxw.base.utils.util.MapUtils;
import com.Txunda.fxw.ui.adapter.BottomLawAdapter2;
import com.Txunda.fxw.ui.adapter.Law_DetailAdapter;
import com.Txunda.fxw.ui.adapter.model.LawModel;
import com.Txunda.fxw.ui.base.BaseAty;
import com.Txunda.fxw.ui.http.Office;
import com.Txunda.fxw.view.RoundedImageView;
import com.Txunda.fxw.view.dialog.AlertDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author txunda_lh 2017年6月3日10:32:35 律师诉讼保全详情
 */
public class LawServiceDetailAty extends BaseAty {
    @BindView(R.id.aty_title3_icon)
    ImageView atyTitle3Icon;
    @BindView(R.id.aty_title3_tv_center)
    TextView atyTitle3TvCenter;
    @BindView(R.id.aty_title3_tv_right2)
    TextView atyTitle3TvRight2;
    @BindView(R.id.law_recycler)
    RecyclerView lawRecycler;
    @BindView(R.id.tv_court_name)
    TextView tvCourtName;
    @BindView(R.id.tv_legalp)
    TextView tvLegalp;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_content1)
    TextView tvContent1;
    @BindView(R.id.tv_tel1)
    TextView tvTel1;
    @BindView(R.id.tv_phone1)
    TextView tvPhone1;
    @BindView(R.id.tv_content2)
    TextView tvContent2;
    @BindView(R.id.tv_tel2)
    TextView tvTel2;
    @BindView(R.id.tv_phone2)
    TextView tvPhone2;
    @BindView(R.id.tv_wechat)
    TextView tvWechat;
    @BindView(R.id.tv_dianji)
    TextView tvDianji;
    @BindView(R.id.tv_express)
    TextView tvExpress;
    @BindView(R.id.tv_tel)
    TextView tvTel;
    @BindView(R.id.bottom_detail_recycler)
    RecyclerView bottomDetailRecycler;
    @BindView(R.id.scrollView1)
    ScrollView scrollView1;
    @BindView(R.id.open_side_iv)
    RoundedImageView openSideIv;
    @BindView(R.id.side_bar_phone_tv)
    LinearLayout sideBarPhoneTv;
    @BindView(R.id.side_bar_like_iv)
    ImageView sideBarLikeIv;
    @BindView(R.id.side_bar_like_tv)
    TextView sideBarLikeTv;
    @BindView(R.id.side_bar_like_layout)
    LinearLayout sideBarLikeLayout;
    @BindView(R.id.side_bar_evaluate_tv)
    LinearLayout sideBarEvaluateTv;
    @BindView(R.id.side_bar_settings_tv)
    LinearLayout sideBarSettingsTv;
    @BindView(R.id.side_bar_share_tv)
    LinearLayout sideBarShareTv;
    @BindView(R.id.app_side_top_layout)
    LinearLayout appSideTopLayout;
    @BindView(R.id.linear_share2)
    LinearLayout linearShare2;
    @BindView(R.id.linear_weibo)
    LinearLayout linearWeibo;
    @BindView(R.id.linear_weixin)
    LinearLayout linearWeixin;
    @BindView(R.id.linear_friends)
    LinearLayout linearFriends;
    @BindView(R.id.linear_qq)
    LinearLayout linearQq;
    @BindView(R.id.linear_bottom)
    LinearLayout linearBottom;
    @BindView(R.id.drawer_layout)
    DrawerLayout drawerLayout;
    private Office office;
    private String lat, lnt, address, name, pic, url = "";
    private String webText = "";

    private Law_DetailAdapter adapter;
    private List<LawModel> list_data = new ArrayList<>();
    private BottomLawAdapter2 bottomAdapter;

    private String classId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setDrawerLayout(drawerLayout);

        getLikesId(sideBarLikeIv, classId);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_lawservicedetail;
    }

    Map<String, String> data;

    @Override
    protected void requestData() {
        office.preservation(classId, this);
        showProgressDialog();
    }

    @Override
    protected void initialized() {
        classId = getIntent().getStringExtra("id");
        type = "fb" + classId;
        atyTitle3TvCenter.setText("律师诉讼保全");
        atyTitle3TvRight2.setVisibility(View.VISIBLE);
        atyTitle3TvRight2.setText(getIntent().getStringExtra("district"));
        lawRecycler.setLayoutManager(
                new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false));
        lawRecycler.setItemAnimator(new DefaultItemAnimator());
        // 设置固定大小
        lawRecycler.setHasFixedSize(true);
        adapter = new Law_DetailAdapter(LawServiceDetailAty.this);
        // 为底部的recyclerView设置LinearManager管理器对象
        bottomDetailRecycler.setLayoutManager(
                new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false));
        bottomDetailRecycler.setItemAnimator(new DefaultItemAnimator());
        // 设置固定大小
        bottomDetailRecycler.setHasFixedSize(true);
        bottomAdapter = new BottomLawAdapter2(LawServiceDetailAty.this);
        office = new Office();
        setListener();
    }

    private void setListener() {
        tvAddress.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("lat", lat);
                bundle.putString("lnt", lnt);
                bundle.putString("address", address);
                bundle.putString("name", name);
                bundle.putString("pic", pic);
                startActivity(PathAty.class, bundle);
            }
        });

        tvWechat.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse(url);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            }
        });
        tvTel1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                call(tvTel1.getText().toString());
            }
        });
        tvPhone1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                call(tvPhone1.getText().toString());

            }
        });
        tvTel2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                call(tvTel2.getText().toString());
            }
        });
        tvPhone2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                call(tvPhone2.getText().toString());

            }
        });
        tvTel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                /* 快递的电话 */
                call(tvTel.getText().toString());
            }
        });
        tvDianji.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Bundle b = new Bundle();
                b.putString("text", webText);
                b.putString("name", name);
                startActivity(LawWebAty.class, b);
            }
        });
    }


    @Override
    @OnClick({R.id.open_side_iv, R.id.side_bar_share_tv, R.id.linear_share2})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.open_side_iv:
                if (!drawerLayout.isDrawerOpen(Gravity.END))
                    drawerLayout.openDrawer(Gravity.END);
                appSideTopLayout.setVisibility(View.VISIBLE);
                linearBottom.setVisibility(View.GONE);
                break;
            case R.id.side_bar_share_tv:
                appSideTopLayout.setVisibility(View.GONE);
                linearBottom.setVisibility(View.VISIBLE);
                break;
            case R.id.linear_share2:
                appSideTopLayout.setVisibility(View.VISIBLE);
                linearBottom.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        data = JSONUtils.parseKeyAndValueToMap(jsonStr);
        if (MapUtils.isEmpty(data))
            return;
        if (data.get("flag").equals("success")) {
            data = JSONUtils.parseKeyAndValueToMap(data.get("data"));
            setData();
            ArrayList<Map<String, String>> bottom_list_data = JSONUtils.parseKeyAndValueToMapList(data.get("shop_list"));
            if (bottom_list_data.size() == 1) {
                Map<String, String> bld1 = new HashMap<>();
                bld1.put("com_id", "");
                bld1.put("class_id", "");
                bld1.put("name", "");
                bld1.put("type", "0");
                bld1.put("o_id", "");
                bld1.put("district_id", "");
                Map<String, String> bld2 = new HashMap<>();
                bld2.put("com_id", "");
                bld2.put("class_id", "");
                bld2.put("name", "");
                bld2.put("type", "0");
                bld2.put("o_id", "");
                bld2.put("district_id", "");
                bottom_list_data.add(0, bld2);
                bottom_list_data.add(0, bld1);
            } else if (bottom_list_data.size() == 2) {
                Map<String, String> bld3 = new HashMap<>();
                bld3.put("com_id", "");
                bld3.put("class_id", "");
                bld3.put("name", "");
                bld3.put("type", "0");
                bld3.put("o_id", "");
                bld3.put("district_id", "");
                bottom_list_data.add(0, bld3);
            }
            bottomAdapter.setList(bottom_list_data);
            bottomDetailRecycler.setAdapter(bottomAdapter);
        }

    }

    private void setData() {
        LawModel lm;
        if (data.get("take_card").equals("1")) {
            lm = new LawModel();
            lm.setImg(R.drawable.icon_quzheng);
            lm.setTitle("取证");
            list_data.add(lm);
        }
        if (data.get("change_files").equals("1")) {
            lm = new LawModel();
            lm.setImg(R.drawable.icon_diaodang);
            lm.setTitle("调档");
            list_data.add(lm);
        }
        if (data.get("use_files").equals("1")) {
            lm = new LawModel();
            lm.setImg(R.drawable.icon_zuanjuan);
            lm.setTitle("组案卷");
            list_data.add(lm);
        }
        if (data.get("record").equals("1")) {
            lm = new LawModel();
            lm.setImg(R.drawable.icon_lian);
            lm.setTitle("立案");
            list_data.add(lm);
        }
        if (data.get("bid").equals("1")) {
            lm = new LawModel();
            lm.setImg(R.drawable.icon_toubiao);
            lm.setTitle("投标");
            list_data.add(lm);
        }
        if (data.get("refer").equals("1")) {
            lm = new LawModel();
            lm.setImg(R.drawable.icon_zixun);
            lm.setTitle("咨询");
            list_data.add(lm);
        }
        if (data.get("writ").equals("1")) {
            lm = new LawModel();
            lm.setImg(R.drawable.icon_write_wenshu);
            lm.setTitle("写文书");
            list_data.add(lm);
        }
        if (data.get("agency").equals("1")) {
            lm = new LawModel();
            lm.setImg(R.drawable.icon_daili);
            lm.setTitle("代理");
            list_data.add(lm);
        }
        if (data.get("shou").equals("1")) {
            lm = new LawModel();
            lm.setTitle("保全");
            lm.setImg(R.drawable.icon_lh_baoquan);
            list_data.add(lm);
        }
        if (data.get("tea").equals("1")) {
            lm = new LawModel();
            lm.setTitle("茶园");
            lm.setImg(R.drawable.icon_lh_chayuan);
            list_data.add(lm);
        }
        if (data.get("music").equals("1")) {
            lm = new LawModel();
            lm.setTitle("音乐");
            lm.setImg(R.drawable.icon_lh_yinyue);
            list_data.add(lm);
        }
        adapter.setList(list_data);
        lawRecycler.setAdapter(adapter);
        tvAddress.setText(data.get("address"));
        tvCourtName.setText(data.get("company"));
        tvLegalp.setText(data.get("legalp"));
        tvContent1.setText(data.get("content1"));
        tvTel1.setText(data.get("tel1"));
        tvPhone1.setText(data.get("phone1"));
        tvContent2.setText(data.get("content2"));
        tvTel2.setText(data.get("tel2"));
        tvPhone2.setText(data.get("phone2"));
        tvWechat.setText(data.get("wechat"));
        tvDianji.setText(data.get("dianji"));
        tvExpress.setText(data.get("express"));
        tvTel.setText(data.get("tel"));
        name = data.get("name");
        address = data.get("address");
        lat = data.get("lat");
        lnt = data.get("lnt");
        pic = data.get("pic");
        url = data.get("wechat_url");
        webText = data.get("dianji_detail");
    }

    private void call(final String tel) {
        new AlertDialog(this).builder().setTitle("提示").setMsg("tel: " + tel)
                .setPositiveButton("确认", new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel: " + tel));
                        startActivity(intent);

                    }
                }).setNegativeButton("取消", new OnClickListener() {

            @Override
            public void onClick(View v) {

            }
        }).show();
    }
}
